/**
 * 由 WangYu 创建
 * 日期 2017/9/5
 */
import React, {Component} from 'react';
import {
    StyleSheet,
    BackHandler,
    NativeModules,
    ToastAndroid, View, TouchableOpacity, Image, Text
} from 'react-native';
import CardStackStyleInterpolator from 'react-navigation/src/views/CardStackStyleInterpolator';
import SplashPage from "./ui/other/SplashPage";
import WebViewPage from "./ui/other/WebViewPage";
import {Router, Scene, Reducer, Actions} from "react-native-router-flux";
import MainPage from "./ui/other/MainPage";
import LoginPage from "./ui/other/LoginPage";
import {navigationBarColor} from "./util/Config";
import RegisterPage from "./ui/other/RegisterPage";
import MessageListPage from "./ui/more/MessageListPage";
import FeedBackPage from "./ui/more/FeedBackPage";
import AboutUsPage from "./ui/more/AboutUsPage";
import RealNamePage from "./ui/authority/RealNamePage";

const reducerCreate = params => {
    const defaultReducer = new Reducer(params);
    return (state, action) => {
        // console.log(state);
        // console.log(action);
        if (action.type === 'Navigation/BACK' && state.routes[state.routes.length - 1].routeName === "MainPage") {
            if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
                //最近2秒内按过back键，可以退出应用。
                // NativeModules.MyNativeMoudle.exitApp();
                BackHandler.exitApp();
                return;
            }
            this.lastBackPressed = Date.now();
            ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT);
        }
        return defaultReducer(state, action);
    };
};
const styles = StyleSheet.create({
    defaultLeftIconStyle: {
        width: 60,
        // backgroundColor:'green',
        justifyContent: 'center',
        alignItems: 'center',
        flexDirection: 'row',
        paddingLeft:10
    },
    defaultRightIconStyle: {
        width: 60,
    }
});
export default class App extends Component {

    constructor(props) {
        super(props);
    }

    render() {
        return <Router createReducer={reducerCreate}>
            <Scene key="root"
                   titleStyle={{alignSelf: 'center', color: 'white',fontWeight:'normal'}}
                   navigationBarStyle={{
                       backgroundColor: navigationBarColor,
                       height: 44,
                       elevation: 0,
                   }}
                   renderLeftButton={(
                       <TouchableOpacity onPress={() => {
                           Actions.pop()
                       }}>
                           <View style={styles.defaultLeftIconStyle}>
                               <Image source={require('../images/fh.png')} style={{height: 15, width: 15}}/>
                               <Text style={{color: 'white', fontSize: 16,marginLeft:5}}>返回</Text>
                           </View>
                       </TouchableOpacity>)}
                   renderRightButton={(
                       <View style={styles.defaultRightIconStyle}/>
                   )}
                   gesturesEnabled
                   transitionConfig={() => ({screenInterpolator: CardStackStyleInterpolator.forHorizontal})}>

                <Scene key="Splash" component={SplashPage} title="splash" initial hideNavBar/>
                <Scene key="MainPage" component={MainPage} hideNavBar/>
                <Scene key="WebViewPage" component={WebViewPage} hideNavBar/>
                <Scene key="LoginPage" component={LoginPage} hideNavBar/>
                <Scene key="RegisterPage" component={RegisterPage} hideNavBar/>
                <Scene key="MessageListPage" component={MessageListPage} title='消息中心'/>
                <Scene key="FeedBackPage" component={FeedBackPage} title='意见反馈'/>
                <Scene key="AboutUsPage" component={AboutUsPage} title='关于我们'/>
                <Scene key="RealNamePage" component={RealNamePage} title='实名认证'/>
            </Scene>
        </Router>
    }
}






